package login;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashUtil {
	
	// vi bruger sha-1 til at hashe passwords
	public static final String Algorithm = "SHA-1";
	
	private static MessageDigest digest;
	
	private final static String hexValues = "0123456789ABCDEF";
	
	static {
		try {
			digest = MessageDigest.getInstance(Algorithm);
		} catch (NoSuchAlgorithmException e) {
			
			// vi er garanteret at SHA-1 findes
			e.printStackTrace();
		}
	}
	
	private static String toHexString(byte[] hashBytes) {
		StringBuilder hb = new StringBuilder(2 * hashBytes.length);
		
		for (byte b : hashBytes) {
			hb.append(hexValues.charAt((b & 0xF0) >> 4));
			hb.append(hexValues.charAt(b & 0x0F));
		}
		
		return hb.toString();
	}
	

	public static String hexHash(String clearText) {
		digest.reset();
		
		byte[] hashBytes = digest.digest(clearText.getBytes());
		
		return toHexString(hashBytes);
	}
}